<?php
//Start de sessie
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Home</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <link type="text/css" href="css/pagination.css" rel="stylesheet" />
  <link rel="stylesheet" href="css/reset.css" type="text/css" />
  <link rel="stylesheet" href="css/960.css" type="text/css" />
  <link rel="stylesheet" href="css/text.css" type="text/css" />
  <link type="text/css" href="css/tablesorter.css" rel="stylesheet" />
  <link type="text/css" href="css/sunny/jquery-ui-1.8.custom.css" rel="stylesheet" />
  <link type="text/css" href="css/jquery.autocomplete.css" rel="stylesheet" />
  <link type="text/css" href="css/style.css" rel="stylesheet" />
  <link type="text/css" href="css/accmenu.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.4.2.min.js">
</script>
<script type="text/javascript" src="js/jquery-ui-1.8.custom.min.js">
</script>
<script type="text/javascript" src="js/jquery.tablesorter.js">
</script>
<script type="text/javascript" src="js/jquery.autocomplete.min.js">
</script>
<script type="text/javascript" src="js/menu.js">
</script>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function()
    {
        $("#producttable").tablesorter();
        $("#searchtext").autocomplete("autocomplete.php");
        $("#searchtext").result(function(event, data, formatted) {
			$("#searchform").trigger('submit');
	    });

        $("#loginbox").dialog({
			autoOpen: false,
			height: 230,
			width: 350,
			modal: true,
            buttons: {
				'Login': logincall,
				Cancel: function() {
					$(this).dialog('close');
				}
                }
		});

        $("#login button").button({
            icons: {
                primary: 'ui-icon-locked'
            }
        })
        .click(function() {
				$('#loginbox').dialog('open');
			});
        $("#register button").button();
        $("#logout button").button();
        $("#shoppingcart button").button();
    }
);

function logincall() {
      $.ajax({
      type: "POST",
      url: "login.php",
      data: $("#loginform").serialize(),
      dataType: "text",
      complete: function() {
        $(this).dialog('close');
        $(location).attr('href', "index.php");
      }
    });
  return;
}

//]]>
</script>
</head>

<body>
  <div class="container_12">
    <div id="header" class="grid_12">
      <div class="grid_5 prefix_7 alpha omega">
        <form id="searchform" name="searchform" method="get" action="search.php">
          <input type="text" name="q" id="searchtext" /> <button type="submit" id=
          "searchbutton"></button>
        </form>

        <div id="demo">
          <ul>
            <?php if(!empty($_SESSION['klantid'])){?>
            <li id="logout"><a href="logout.php"><button>Log Out</button></a></li>
            <li id="shoppingcart"><a href="view_cart.php"><button>Winkelwagen</button></a></li>
            <?php } else {?>
            <li id="login"><button>Log In</button></li>
            <li id="register"><a href="register.php"><button>Registreer</button></a></li>
            <?php } ?>
          </ul>


        </div>
        <?php if(!empty($_SESSION['klantid'])){?>
                        <p id="klantwelkom">Welkom <b><a href="account.php"><?php echo $_SESSION['fullname']; ?></a></b>&nbsp;&nbsp;
                        </p><?php } else { ?>
                        <p id="klantwelkom"><a href="forgot_pass.php">Wachtwoord vergeten?</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
                        <?php } ?>
      </div>

      <div class="clear"></div>
    </div>

    <div class="clear"></div>

    <div class="grid_12 spacer"></div>

    <div class="clear"></div>

    <div id="menu" class="grid_2">


          <?php
          include_once 'temp/menu_filler.php';
          ?>

     </div>

    <div id="mainarealong" class="grid_7">
        <div id="tableholder">

                      <?php
                      	/*
                      		Place code to connect to your DB here.
                      	*/
                      	include('includes/connect.php');	// include your code to connect to DB.

                      	$tbl_name="product";		//your table name
                      	// How many adjacent pages should be shown on each side?
                      	$adjacents = 3;

                      	/*
                      	   First get total number of rows in data table.
                      	   If you have a WHERE clause in your query, make sure you mirror it here.
                      	*/

                        $now=mktime();

                      	$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE begindate <= '".$now."' AND enddate >= '".$now."' ";
                      	$total_pages = mysql_fetch_array(mysql_query($query));
                      	$total_pages = $total_pages[num];
                        $aa = $total_pages;

                      	/* Setup vars for query. */
                      	$targetpage = "index.php"; 	//your file name  (the name of this file)
                      	$limit = 10; 								//how many items to show per page
                      	$page = $_GET['page'];
                      	if($page)
                      		$start = ($page - 1) * $limit; 			//first item to display on this page
                      	else
                      		$start = 0;								//if no page var is given, set start to 0

                      	/* Get data. */
                      	$sql = "SELECT * FROM product WHERE begindate <= '".$now."' AND enddate >= '".$now."' ORDER BY RAND() LIMIT 10";
                      	$result = mysql_query($sql);

                      	/* Setup page vars for display. */
                      	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
                      	$prev = $page - 1;							//previous page is page - 1
                      	$next = $page + 1;							//next page is page + 1
                      	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
                      	$lpm1 = $lastpage - 1;						//last page minus 1

                      	/*
                      		Now we apply our rules and draw the pagination object.
                      		We're actually saving the code to a variable in case we want to draw it more than once.
                      	*/
                      	$pagination = "";
                      	if($lastpage > 1)
                      	{
                      		$pagination .= "<div class=\"pagination\">";
                      		//previous button
                      		if ($page > 1)
                      			$pagination.= "<a href=\"$targetpage?page=$prev\">« previous</a>";
                      		else
                      			$pagination.= "<span class=\"disabled\">« previous</span>";

                      		//pages
                      		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
                      		{
                      			for ($counter = 1; $counter <= $lastpage; $counter++)
                      			{
                      				if ($counter == $page)
                      					$pagination.= "<span class=\"current\">$counter</span>";
                      				else
                      					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                      			}
                      		}
                      		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
                      		{
                      			//close to beginning; only hide later pages
                      			if($page < 1 + ($adjacents * 2))
                      			{
                      				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                      				{
                      					if ($counter == $page)
                      						$pagination.= "<span class=\"current\">$counter</span>";
                      					else
                      						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                      				}
                      				$pagination.= "...";
                      				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                      				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
                      			}
                      			//in middle; hide some front and some back
                      			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
                      			{
                      				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                      				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                      				$pagination.= "...";
                      				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                      				{
                      					if ($counter == $page)
                      						$pagination.= "<span class=\"current\">$counter</span>";
                      					else
                      						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                      				}
                      				$pagination.= "...";
                      				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                      				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
                      			}
                      			//close to end; only hide early pages
                      			else
                      			{
                      				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                      				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                      				$pagination.= "...";
                      				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                      				{
                      					if ($counter == $page)
                      						$pagination.= "<span class=\"current\">$counter</span>";
                      					else
                      						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                      				}
                      			}
                      		}

                      		//next button
                      		if ($page < $counter - 1)
                      			$pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
                      		else
                      			$pagination.= "<span class=\"disabled\">next »</span>";
                      		$pagination.= "</div>\n";
                      	}
                      ?>
                        <h3>Huidige aanbiedingen</h3>
                        Er zijn momenteel <b><?php echo $aa; ?></b> producten in de aanbieding.
                      	<?php
                              echo "<table class='tablesorter' id='producttable' border='1' CELLPADDING=25 STYLE='font-size:16px'>";
                              echo "<thead><tr> <th>afbeelding</th> <th>productnaam</th> <th>prijs</th><th>actieprijs</th></tr></thead>";
                              echo "<tbody>";
                              // keeps getting the next row until there are no more to get
                              while ($row = mysql_fetch_array($result)) {

                              $id=$row['productid'];
                              $naam=$row['productnaam'];
                              $cat=$row['categorie'];

                              if($id<='166526')
                              {
                                $img='productimages/'.$id.'.jpg';
                              }
                              else
                              {
                                $img=$row['img'];
                              }

                              $promoprice=$row['Promotieprijs'];
                              $price=$row['stdprijs'];

                              // Print out the contents of each row into a table
                              echo    "<tr><td>".
                                      "<img src=\"img/$img\" width=\"75\" height=\"75\">".
                                      "</td><td>".
                                      "<a href=\"product.php?id=$id \">$naam</a>".
                                      "</td><td>".
                                      "&euro;".
                                      number_format($price,2).
                                      "</td><td>".
                                      "<b>&euro;".
                                      number_format($promoprice,2).
                                      "</b></td></tr>";
                              }
                              echo    "</tbody>".
                                      "</table>";

                            ?>

        </div>
    </div>

    <div id="sidebar" class="grid_3"><div id="tableholder"><?php include 'cart.php'; ?></div></div>

    <div class="clear"></div>

    <div class="grid_12 spacer"></div>

    <div class="clear"></div>
</div>
<div id="loginbox">
                <form action="#" method="POST" id="loginform">
                <p><label for="email">Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                <input type="text" value="" name="email" class="text ui-widget-content ui-corner-all" /></p>
                <p><label for="wachtwoord">Wachtwoord:</label>
                <input type="password" value="" name="wachtwoord" class="text ui-widget-content ui-corner-all" /></p>
</form>
</div>
<center>
<!-- Begin Motigo Webstats counter code -->
<a id="mws4736564" href="http://webstats.motigo.com/">
<img width="80" height="15" border="0" alt="Free counter and web stats" src="http://m1.webstats.motigo.com/n80x15.gif?id=AEhGNAHBHVZi00ZSyCakWn8n38NA" /></a>
<script src="http://m1.webstats.motigo.com/c.js?id=4736564&amp;lang=NL&amp;i=3" type="text/javascript"></script>
<!-- End Motigo Webstats counter code -->
</center>
<br />

</body>
</html>